layui.use(['customUtil'], function () {
    var customUtil = layui.customUtil;

    var weather = {

        _constans: {
            key: "7f4cccef4fa4408ab8fc067b2afa6e52",
            appid: "24723993",
            appsecret: "1FPUvmi8",
            version: "v9",
            api: {
                weatherApi1:"https://free-api.heweather.net/s6/weather/forecast?",
                weatherApi2:appCtx+  "/queryWeather"
            }
        },

        _init: function () {
            let _this = this;
            _this._getWeatherData2();
        },

        _getQueryParam1: function (cityCode) {
            let _this = this;
            return "location=" + cityCode + "&key=" + _this._constans.key;
        },

        _getQueryParam2: function (cityCode) {
            let _this = this;
            let data = {
                "appid": _this._constans.appid,
                "appsecret": _this._constans.appsecret,
                "version": _this._constans.version,
                "callback": "jsonp",
                "cityid": cityCode
            };
            return data;
        },

        _getWeatherData1: function () {
            let _this = this;
            let queryUrl = _this._constans.api.weatherApi1 + _this._getQueryParam1("CN101190101");
            customUtil.get(queryUrl, function (data) {
                let chartData = _this._convertData1(data);
                _this._initWeatherCharts(chartData);
            });

        },

        _getWeatherData2: function () {
            let _this = this;

            customUtil.post(_this._constans.api.weatherApi2, _this._getQueryParam2("CN101190101"), function (data) {
                let chartData = _this._convertData2(data);
                _this._initWeatherCharts(chartData);
            });

        },

        _convertData1: function (data) {
            let title = "";
            let legendData = ["最高温度", "最低温度"];
            let seriesData = [
                {
                    name: '最高温度',
                    type: 'line',
                    data: []
                },
                {
                    name: '最低温度',
                    type: 'line',
                    data: []
                }
            ];
            let xAxisData = [];

            if (data.HeWeather6 && data.HeWeather6.length > 0 && data.HeWeather6[0].status === "ok") {
                let dailyData = data.HeWeather6[0].daily_forecast;
                title = data.HeWeather6[0].basic.location + "天气";
                for (let i = 0; i < dailyData.length; i++) {
                    xAxisData[xAxisData.length] = dailyData[i].date;
                    seriesData[0].data[seriesData[0].data.length] = dailyData[i].tmp_max;
                    seriesData[1].data[seriesData[1].data.length] = dailyData[i].tmp_min;
                }
            }

            let option = {
                title: {
                    text: title
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: legendData
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: true,
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: seriesData
            };
            return option;

        },

        _convertData2: function (data) {
            let title = "";
            let legendData = ["最高温度", "最低温度"];
            let seriesData = [
                {
                    name: '最高温度',
                    type: 'line',
                    data: []
                },
                {
                    name: '最低温度',
                    type: 'line',
                    data: []
                }
            ];
            let xAxisData = [];

            if (data.result.data && data.result.data.length > 0 ) {
                let dailyData = data.result.data;
                title = data.result.city + "天气";
                for (let i = 0; i < dailyData.length; i++) {
                    xAxisData[xAxisData.length] = dailyData[i].date;
                    seriesData[0].data[seriesData[0].data.length] = dailyData[i].tem1;
                    seriesData[1].data[seriesData[1].data.length] = dailyData[i].tem2;
                }
            }

            let option = {
                title: {
                    text: title
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: legendData
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: true,
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: seriesData
            };
            return option;

        },

        _initWeatherCharts: function (chartData) {
            let myChart = echarts.init(document.getElementById('chart'));
            myChart.setOption(chartData);
            $(window).resize(function () {
                myChart.resize();
            });
        }

    };

    weather._init();

});